package org.dftproject.genesis.ui.pages.summary.sources;

import java.util.HashSet;
import java.util.Set;

import org.dftproject.genesis.data.IAttribute;
import org.dftproject.genesis.data.genealogy.GenealogyConstants;
import org.dftproject.genesis.data.genealogy.GenealogyUtils;
import org.dftproject.genesis.data.genealogy.ISource;
import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.citations.ICitation;
import org.dftproject.genesis.ui.citations.ICitationFormatter;
import org.dftproject.genesis.ui.figures.ColumnData;
import org.dftproject.genesis.ui.figures.ColumnLayout;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.Figure;

public class Footnote extends Figure {
	
	private final int number;
	private final ISource source;
	private final Set<IAttribute> attributes = new HashSet<IAttribute>();
	
	public Footnote(int number, ISource source) {
		this.number = number;
		this.source = source;
		
		ColumnLayout layout = new ColumnLayout();
		layout.setSpacing(10);
		setLayoutManager(layout);
		
		FootnoteBullet bullet = new FootnoteBullet(number);
		add(bullet, new ColumnData());
		
		FootnoteText footnoteText = new FootnoteText();
		
		String type = (String) GenealogyUtils.getValue(source, GenealogyConstants.type, String.class);
		format(footnoteText, source, type);
		
		add(footnoteText, new ColumnData(1));
	}

	protected void format(final FootnoteText footnoteText, ISource source, String type) {
		ICitation citation = new ICitation() {

			public void addHyperlink(String text, IHyperlinkListener listener) {
				footnoteText.addHyperlink(text, listener);
			}

			public void addText(String text) {
				footnoteText.addText(text);
			}

			public void addHyperlink(String text, IHyperlinkListener listener, String tooltip) {
				footnoteText.addHyperlink(text, listener, tooltip);
			}
			
		};
		
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(Activator.ID, Activator.CITATION_FORMATTERS_EXTENSION_POINT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < configElements.length; j++) {
				if (type.equals(configElements[j].getAttribute("type"))) {
					try {
						Object formatter = configElements[j].createExecutableExtension("class");
						if (formatter instanceof ICitationFormatter)
							((ICitationFormatter) formatter).format(citation, source);
					} catch (CoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}

	public int getNumber() {
		return number;
	}

	public void addAttribute(IAttribute sourceAttribute) {
		attributes.add(sourceAttribute);
	}

}
